﻿using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 属性解析器
    /// </summary>
    public class LimitPropsContractResolver : DefaultContractResolver
    {
        /// <summary>
        /// 是否禁用JsonIgnore特性
        /// </summary>
        public bool IsDisenableJsonIgnore { get; set; }

        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            return base.CreateProperties(type, memberSerialization).Where(delegate (JsonProperty p)
            {
                if (this.IsDisenableJsonIgnore || p.AttributeProvider.GetAttributes(typeof(MessageJsonIncludeAttribute), true).Count > 0)
                {
                    p.Ignored = false;
                }
                return p.AttributeProvider.GetAttributes(typeof(MessageJsonIgnoreAttribute), true).Count <= 0 || p.AttributeProvider.GetAttributes(typeof(MessageJsonIncludeAttribute), true).Count > 0;
            }).ToList<JsonProperty>();
        }
    }
}
